// 使用 mysql2/promise 的正确方式
const mysql = require('mysql2/promise');

const pool = mysql.createPool({
  host: process.env.DB_HOST || 'localhost',
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASS || 'root',
  database: process.env.DB_NAME || 'short_link',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
});

// 测试连接
async function testConnection() {
  const connection = await pool.getConnection();
  try {
    await connection.ping();
    console.log('成功连接到MySQL数据库');
  } finally {
    connection.release();
  }
}

testConnection().catch(err => {
  console.error('数据库连接失败:', err);
  process.exit(1);
});

module.exports = pool;